package ai;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImgBinary
{
	public static void main(String [] args) throws IOException
	{
		BufferedImage bi = ImageIO.read(new File("D:/全屏截图-2.png"));//通过imageio将图像载入  
		int h = bi.getHeight();//获取图像的高  
		int w = bi.getWidth();//获取图像的宽  
//		int rgb = bi.getRGB(0, 0);//获取指定坐标的ARGB的像素值  
		int [][] gray = new int[w][h];
		for(int x = 0; x < w; x++)
		{
			for(int y = 0; y < h; y++)
			{
				gray[x][y] = bi.getRGB(x, y);
			}
		}
		BufferedImage nbi = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
		BufferedImage mbi = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
		int max = new Color(255, 255, 255).getRGB();
		int middle = new Color(225, 125, 125).getRGB();
		int min = new Color(0, 0, 0).getRGB();
		for(int x = 0; x < w; x++)
		{
			for(int y = 0; y < h; y++)
			{
				String str = Integer.toHexString(gray[x][y]);
				int r = Integer.parseInt(str.substring(2, 4), 16);
				int g = Integer.parseInt(str.substring(4, 6), 16);
				int b = Integer.parseInt(str.substring(6, 8), 16);
				if((r > 44) && (r < 130) && (g > 170) && (g < 255) && (b > 17) && (b < 130))
				{
					nbi.setRGB(x, y, max);
				}else
				{
					nbi.setRGB(x, y, min);
				}
				if((r > 170) && (r < 200) && (g > 210) && (g < 240) && (b > 220) && (b < 240))
				{
					mbi.setRGB(x, y, middle);
				}else
				{
					mbi.setRGB(x, y, min);
				}
			}
		}
		ImageIO.write(nbi, "png", new File("D:/全屏截图-2_水滴图.png"));
		ImageIO.write(mbi, "png", new File("D:/全屏截图-2_外框图.png"));
	}
}
